// 该文件专门用于创建整个应用的路由器
import VueRouter from "vue-router";
//引入组件
import About from "../pages/About";
import Home from "../pages/Home";
import News from "../pages/News";
import Message from "../pages/Message";
import Detail from "../pages/Detail";

//创建并暴露一个路由器
const router = new VueRouter({
  mode: "history",
  routes: [
    { name: "guanyu", path: "/about", component: About },
    {
      name: "zhuye",
      path: "/home",
      component: Home,
      children: [
        {
          name: "xinwen",
          path: "news",
          component: News,
        },
        {
          name: "xiaoxi",
          path: "message",
          component: Message,
          children: [
            {
              path: "detail",
              name: "xiangqing",
              component: Detail,
              // props的第一种写法，值为对象，改对象中所以key-value都会以props的形式传给detail组件

              // props:{
              // 	a:1,b:'nh'
              // }

              // props的第二种写法，为布尔值，值为真，会把改路由组件收到的params参数，以props的形式传给detail组件
              // props:true

              // props的第三种写法，值为函数
              props($route) {
                return { id: $route.query.id, title: $route.query.title };
              },
            },
          ],
        },
      ],
    },
  ],
});

router.beforeEach((to, from, next) => {
  console.log(to, from);
  if (to.name === "xinwen" || to.name === "xiaoxi") {
    if (localStorage.getItem("school") === "atguigu") {
      next();
    } else {
      alert("学校名不对，无权访问！");
    }
  } else {
    next();
  }
});

export default router;
